package com.jcoinexchange.match.engine;

import com.lmax.disruptor.EventHandler;

public class CancelOrderEventHandler implements EventHandler<OrderEvent> {
    @Override
    public void onEvent(OrderEvent event, long sequence, boolean endOfBatch) {
        if (event.getType() != EventType.CANCEL_ORDER) return;
        OrderBook orderBook = new OrderBook();

        Order order = orderBook.getOrderIdIndex().get(event.getOrderId());
        if (order == null || !order.getStatus().isCancelable()) return;

        // 加锁保证原子性
        synchronized (orderBook) {
            orderBook.removeOrder(order);
            order.setStatus(OrderStatus.CANCELED);
            // 解冻资产（异步通知账户服务）
            notifyAccountServiceUnfreeze(order);
        }
    }
}
